package com.itboye.eweilisten.main;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.view.View;
import android.widget.Button;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.app.MyApplication;
import com.itboye.eweilisten.me.login.LoginActivity;
import com.itboye.eweilisten.presenter.UserPresenter;
import com.itboye.eweilisten.util.sputil.Const;
import com.itboye.eweilisten.util.sputil.SPUtils;
import com.itboye.eweilisten.util.widget.IsStringSPUtils;
import com.itboye.eweilisten.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

/**
 * Created by itboye on 2017/4/1.
 * <p>
 * 启动页
 */

public class StartUpActivity extends Activity implements Observer {
    Button button2;
    private MyCountDownTimer mc;
    UserPresenter presenter;
    String auto_login_code;
    String uid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_start);
        presenter = new UserPresenter(this);
        button2 = (Button) findViewById(R.id.button2);

        onRun();
        /***
         * 设置广告时长
         */
        mc = new MyCountDownTimer(3000, 1000);
        mc.start();
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(StartUpActivity.this, MainActivity.class));
                StartUpActivity.this.finish();
                handler.removeMessages(0);
            }
        });
    }

    public void onRun() {
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
//                SharedPreferences setting = getSharedPreferences("document", 0);
//                Boolean user_first = setting.getBoolean("FIRST", true);
//                if (user_first) {// 第一次
//                    Intent intent = new Intent(StartUpActivity.this, ActivityDiaoCha.class);
//                    startActivity(intent);
//                    setting.edit().putBoolean("FIRST", false).commit();
//                    StartUpActivity.this.finish();
//                } else {
                auto_login_code= (String) SPUtils.get(MyApplication.instance,SPUtils.DEFAULT_FILE_NAME,"auto_login_code","0");
                uid = (String) SPUtils.get(MyApplication.instance, null, Const.ID, "");
                presenter.onAutoLogin(uid,auto_login_code);

//                }
            }
        }, 2800);
    }

    private Handler handler = new Handler();

    @Override
    public void update(Observable o, Object arg) {
        ResultEntity entity = (ResultEntity) arg;
        if (entity != null) {
            if (entity.getEventType() == presenter.ZiDong_success) {
                startActivity(new Intent(this,MainActivity.class));
                finish();
            }else {
                Intent intent=new Intent(this, LoginActivity.class);
                intent.putExtra("main",true);
                startActivity(intent);
                finish();
            }
        }
    }

    class MyCountDownTimer extends CountDownTimer {
        /**
         * @param millisInFuture    表示以毫秒为单位 倒计时的总数
         *                          <p>
         *                          例如 millisInFuture=1000 表示1秒
         * @param countDownInterval 表示 间隔 多少微秒 调用一次 onTick 方法
         *                          <p>
         *                          例如: countDownInterval =1000 ; 表示每1000毫秒调用一次onTick()
         */
        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        public void onFinish() {
            button2.setText("正在跳转");
        }

        public void onTick(long millisUntilFinished) {
            button2.setText(millisUntilFinished / 1000 + "秒跳过");
        }
    }
}
